# coding:utf-8

from flask import Flask
from kgcar.model.neo4j_models import Neo4jConfig

app = Flask(__name__)

#实体查询
@app.route("/search_entity")
def search_entity(request):
	ctx = {}
	#根据传入的实体名称搜索出关系
	if(request.GET):
		entity = request.GET['user_text']
		#连接数据库
		db = Neo4jConfig
		entityRelation = db.getEntityRelationbyEntity(entity)
		if len(entityRelation) == 0:
			#若数据库中无法找到该实体，则返回数据库中无该实体
			ctx= {'title' : '<h2>数据库中暂未添加该实体</h1>'}
			return render(request,'entity.html'.encode("utf-8"),{'ctx':json.dumps(ctx,ensure_ascii=False)})
		else:
			#返回查询结果
			#将查询结果按照"关系出现次数"的统计结果进行排序
			entityRelation = sortDict(entityRelation)

			return render(request,'entity.html'.encode("utf-8"),{'entityRelation':json.dumps(entityRelation,ensure_ascii=False)})
	#需要进行类型转换
	return render(request,"entity.html".encode("utf-8"),{'ctx':ctx})